﻿/*
 * Copyright 2009 José David Tuero Rodríguez

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 * */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;


namespace AtmosCreator
{
    /// <summary>
    /// Class used to locate the forms texts
    /// </summary>
    public class Localizer
    {
        /// <summary>
        /// XML file used for localizations
        /// </summary>
        private XmlDocument _xml;

        /// <summary>
        /// File source of the localizer
        /// </summary>
        private string _file;

        /// <summary>
        /// Class used to lozalize forms
        /// </summary>
        public Localizer(string file)
        {
            _xml = new XmlDocument();
            _xml.Load(file);
            _file = file;

        }

        /// <summary>
        /// Method used to localizate the tutorial form controls (page dependant)
        /// </summary>
        /// <param name="page">Page of the tutorial to load</param>
        /// <param name="label">Label to put the tutorial texts into</param>
        /// <param name="picture">PictureBox to put a picture into</param>
        public void LocalizeTutorial(int page,Label label,PictureBox picture)
        {
            XmlNodeList tutorialPage = _xml.GetElementsByTagName("Page" + page.ToString());
            foreach (XmlElement nodoTutorial in tutorialPage)
            {
                label.Text = nodoTutorial.GetElementsByTagName("Text")[0].InnerText ;
                XmlNodeList pic = nodoTutorial.GetElementsByTagName("Picture");
                if (pic.Count > 0)
                {
                    switch (pic[0].InnerText)
                    {
                        case "Tuto1.JPG":
                            picture.Image = AtmosCreator.Properties.Resources.tuto1;
                            break;
                        case "Tuto2.JPG":
                            picture.Image = AtmosCreator.Properties.Resources.tuto2;
                            break;
                        case "Tuto3.JPG":
                            picture.Image = AtmosCreator.Properties.Resources.tuto3;
                            break;
                        case "Tuto4.JPG":
                            picture.Image = AtmosCreator.Properties.Resources.tuto4;
                            break;
                        case "Tuto5.JPG":
                            picture.Image = AtmosCreator.Properties.Resources.tuto5;
                            break;
                        default:
                            picture.Image = AtmosCreator.Properties.Resources.logo;
                            break;
                    }
                }
                else
                {
                    picture.Image = AtmosCreator.Properties.Resources.logo;
                }
            }
        }

        /// <summary>
        /// Is the page the last of the tutorial?
        /// </summary>
        /// <param name="page">Page to check</param>
        /// <returns>true if the page is the last one, false if not</returns>
        public bool LastPageTutorial(int page)
        {
            page++;
            XmlNodeList tutorialPage = _xml.GetElementsByTagName("Page" + page.ToString());
            if (tutorialPage.Count > 0)
            {
                return false;
            }
            return true;
        }


        /// <summary>
        /// Method used to get a message error
        /// </summary>
        /// <returns>The error to show</returns>
        public string GetError(string error)
        {
            XmlNodeList errors = _xml.GetElementsByTagName(error);
            foreach (XmlElement nodo in errors)
            {
                return nodo.InnerText;
            }
            return "Error";
        }


        /// <summary>
        /// Method used to lozalize the controls of a form
        /// </summary>
        /// <param name="frm">Form to localize</param>
        public void LocalizeForm(Form frm)
        {
            XmlNodeList forms = _xml.GetElementsByTagName(frm.Name);
            //loop through nodes 
            foreach (XmlElement nodo in forms)
            {                
                XmlNodeList controls = nodo.GetElementsByTagName("Control");
                //loop through nodes 
                foreach (XmlElement nodoControl in controls)
                {
                    //Find our control in the form
                    Control[] control = frm.Controls.Find(nodoControl.GetElementsByTagName("ControlName")[0].InnerText, true);
                    if (control.Length > 0)
                    {
                        control[0].Text = nodoControl.GetElementsByTagName("ControlText")[0].InnerText;
                    }
                    else
                    {
                        //Maybe inside a menuStrip?
                        foreach (Control menu in frm.Controls)
                        {
                            //Dont make this for non menustrip controls
                            if (menu is MenuStrip)
                            {
                                ToolStripItem[] item = ((MenuStrip)menu).Items.Find(nodoControl.GetElementsByTagName("ControlName")[0].InnerText, true);
                                if (item.Length > 0)
                                {
                                    item[0].Text = nodoControl.GetElementsByTagName("ControlText")[0].InnerText;
                                }
                            }
                        }
                    }
                }
            }
        }


        /// <summary>
        /// get and set methods
        /// </summary>
        public string File
        {
            get
            {
                return _file;
            }
        }
    }
}
